package com.hsy.baoinbao.modules.main.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.fragment.DPBaseFragment;
import com.deparse.dpframe.ui.view.DPAutoWrapLinearLayout;
import com.deparse.dpframe.ui.view.DPFullDrawListView;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.main.ui.activity.MainActivity;
import com.hsy.baoinbao.modules.main.ui.activity.PublicAccementActivity;
import com.hsy.baoinbao.modules.main.ui.activity.UserServiceListActivity;
import com.hsy.baoinbao.modules.my.gsondata.PersonalData;
import com.hsy.baoinbao.modules.notice.activity.DetailNoticeActivity;
import com.hsy.baoinbao.modules.notice.activity.NoticeActivity;
import com.hsy.baoinbao.modules.notice.adapter.NoticeAdapter;
import com.hsy.baoinbao.modules.notice.gsondata.NoticeListData;
import com.hsy.baoinbao.modules.order.activity.DetailReceivingActivity;
import com.hsy.baoinbao.modules.order.gsondata.RecyleListData;
import com.hsy.baoinbao.modules.order.view.GarbageItemView;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hsy on 2016/3/23.
 */
public class FragmentHome extends DPBaseFragment implements View.OnClickListener {

    private Button user_service;
    private ImageView mHeadImageImageView;
    private TextView mNickNameTextView;
    private TextView mLevelTextView;
    private TextView money_left, money;
    private LinearLayout mPersonalDataLinearLayout;
    private LinearLayout mYourNextOrderTitleLinearLayout;
    private TextView mDateTimeTextView;
    private TextView mAddrTextView;
    private LinearLayout mYourNextOrderLinearLayout, intent_detail;
    private LinearLayout mPublicAccementTitleLinearLayout;
    private DPFullDrawListView mViewDPFullDrawListView;
    private LinearLayout mPublicAccementListLinearLayout;
    private String intentId;
    private NoticeListData.Result result;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                setPersonalData(response);

                Map<String, String> params = new HashMap<>();
                params.put(Consts.TOKEN, CommonUtils.getToken(getAttchedActivity()));
                params.put(Consts.FIRST_ROW, "0");
                params.put(Consts.LIST_ROWS, "1");
                params.put(Consts.STATUS, "1");
                HttpUtil.httpPostWithParams(getAttchedActivity(), 1, Consts.API_GET_RECYCLE_LIST, params, callback);
            } else if (1 == what) {
                setNextOrder(response);

                Map<String, String> params = new HashMap<>();
                params.put(Consts.TOKEN, CommonUtils.getToken(getAttchedActivity()));
                params.put(Consts.FIRST_ROW, "0");
                params.put(Consts.LIST_ROWS, "5");
                HttpUtil.httpPostWithParams(getAttchedActivity(), 2, Consts.API_GET_NOTICE_LIST, params, callback);
            } else if (2 == what) {
                setNoticeList(response);
                CommonUtils.hideProgressDialog();
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            if (0 == what) {
                Map<String, String> params = new HashMap<>();
                params.put(Consts.TOKEN, CommonUtils.getToken(getAttchedActivity()));
                params.put(Consts.FIRST_ROW, "0");
                params.put(Consts.LIST_ROWS, "1");
                params.put(Consts.STATUS, "1");
                HttpUtil.httpPostWithParams(getAttchedActivity(), 1, Consts.API_GET_RECYCLE_LIST, params, callback);
            } else if (1 == what) {
                Map<String, String> params = new HashMap<>();
                params.put(Consts.TOKEN, CommonUtils.getToken(getAttchedActivity()));
                params.put(Consts.FIRST_ROW, "0");
                params.put(Consts.LIST_ROWS, "5");
                HttpUtil.httpPostWithParams(getAttchedActivity(), 2, Consts.API_GET_NOTICE_LIST, params, callback);
            } else {
                CommonUtils.hideProgressDialog();
            }
        }
    };


    private void setNoticeList(String response) {
        result = DPGsonUtil.fromJson(response, NoticeListData.Result.class);
        NoticeAdapter adapter = new NoticeAdapter(getAttchedActivity(), result.getLists());
        mViewDPFullDrawListView.setAdapter(adapter);
        mPublicAccementListLinearLayout.setVisibility(View.VISIBLE);
    }

    private void setNextOrder(String response) {
        RecyleListData.Result result = DPGsonUtil.fromJson(response, RecyleListData.Result.class);
        if (1 == result.getLists().size()) {
            mDateTimeTextView.setText(result.getLists().get(0).getDate());
            mAddrTextView.setText(result.getLists().get(0).getAddress());
            mYourNextOrderLinearLayout.setVisibility(View.VISIBLE);
            intentId = result.getLists().get(0).getId();
        } else {
            mYourNextOrderLinearLayout.setVisibility(View.GONE);
        }
    }


    private void setPersonalData(String response) {
        PersonalData.Result result = DPGsonUtil.fromJson(response, PersonalData.Result.class);
        Picasso.with(getAttchedActivity()).load(Consts.API_IMAGE_FOLDER_MIDDLE + result.getHead_img()).into(mHeadImageImageView);
        mNickNameTextView.setText(result.getNickname());
        money.setText("本月佣金: " + result.getCommission() + "元");
        money_left.setText("本月服务数: " + result.getMonth_jobs());

        mPersonalDataLinearLayout.setVisibility(View.VISIBLE);
    }

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mHeadImageImageView = (ImageView) findViewById(R.id.head_image);
        mNickNameTextView = (TextView) findViewById(R.id.nick_name);
        mLevelTextView = (TextView) findViewById(R.id.level);
        money = (TextView) findViewById(R.id.money);
        money_left = (TextView) findViewById(R.id.money_left);
        mPersonalDataLinearLayout = (LinearLayout) findViewById(R.id.personal_data);

        mDateTimeTextView = (TextView) findViewById(R.id.date_time);
        mAddrTextView = (TextView) findViewById(R.id.addr);

        mYourNextOrderLinearLayout = (LinearLayout) findViewById(R.id.your_next_order);
        mPublicAccementListLinearLayout = (LinearLayout) findViewById(R.id.public_accement_list);
        intent_detail = F(R.id.intent_detail);

        mYourNextOrderTitleLinearLayout = (LinearLayout) findViewById(R.id.your_next_order_title);
        mPublicAccementTitleLinearLayout = (LinearLayout) findViewById(R.id.public_accement_title);

        user_service = F(R.id.user_service);

        user_service.setOnClickListener(this);
        mYourNextOrderTitleLinearLayout.setOnClickListener(this);
        mPublicAccementTitleLinearLayout.setOnClickListener(this);
        intent_detail.setOnClickListener(this);
        mViewDPFullDrawListView = (DPFullDrawListView) findViewById(R.id.list_view);
        mViewDPFullDrawListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getAttchedActivity(), DetailNoticeActivity.class);
                intent.putExtra("id", result.getLists().get(position).getId());
                startActivity(intent);
            }
        });
        CommonUtils.showProgressDialog(getAttchedActivity());
        Map<String, String> params = new HashMap<>();
        params.put(Consts.TOKEN, CommonUtils.getToken(getAttchedActivity()));
        HttpUtil.httpPostWithParams(getAttchedActivity(), 0, Consts.API_GET_PERSONAL_DATA, params, callback);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onHandleMessage(Message msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.your_next_order_title:
                ((MainActivity) getAttchedActivity()).tab.setCurrentTab(1);
                break;
            case R.id.public_accement_title:
                Intent intent1 = new Intent(getAttchedActivity(), PublicAccementActivity.class);
                startActivity(intent1);
                break;
            case R.id.intent_detail:
                Intent intent = new Intent(getAttchedActivity(), DetailReceivingActivity.class);
                intent.putExtra("id", intentId);
                getAttchedActivity().startActivityForResult(intent, 1);
                break;
            case R.id.user_service:
                Intent intent2 = new Intent(getAttchedActivity(), UserServiceListActivity.class);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }
}
